$(document).ready(function(){

	// jQuery UI specific styles
	$('input[name=studentactivity-to]').datepicker();
	$('input[name=studentactivity-from]').datepicker();
	$('input:checkbox').click(function(){
		$(this).parent().parent().toggleClass('ui-state-highlight');
	});
	
	// Uploadify's options
	$('#studentactivity-file-upload').uploadify({
		'uploader'  : 'inc/uploadify.swf',
		'script'    : 'process/upload_docs.php',
		'cancelImg' : 'css/img/cancel.png',
		'buttonImg'	: 'css/img/folder_add.png',
		'folder'    : 'data/docs',
		'auto'      : false,
		'multi'		: true,
		'wmode'		: 'transparent',
		'height'	: '16',
		'scriptData': { 'otype':'studentactivity', 'id':$('input[name=studentactivity-studentactivityId]').val() },
		'onAllComplete'	: function(event,data){
			location.reload();
		}
	});
	
	// Case: deleting a studentactivity's document
	$('a.delete-doc').click(function(){
		var e = $(this);
		$.ajax({
			type:	'POST',
			url:	'process/document_delete.php',
			data:	'id='+e.attr('href'),
			success: function(msg){
				if (msg != 'OK')
					alert(msg);
				else
					e.parent().parent().remove();
			}
		});
		return false;
	});
	
	// Case: editing studentactivity's contact
	$('input[name=studentactivity-contact]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_contacts.php',
			data:	'id='+$('input[name=studentactivity-contactId]').val(),
			success: function(msg) {
				var select = $('div#select_contact');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0) {
								$('input[name=studentactivity-contact]').val('');
								$('input[name=studentactivity-contactId]').val('');
							} else {
								$('input[name=studentactivity-contact]').val($('span#contact-'+checked.first().val()).html());
								$('input[name=studentactivity-contactId]').val(checked.first().val());
							}
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: editing studentactivity's company
	$('input[name=studentactivity-company]').click(function(){
		var e = $(this);
		e.addClass('loading');
		$.ajax({
			type:	'POST',
			url:	'ajax/load_companies.php',
			data:	'id='+$('input[name=studentactivity-companyId]').val(),
			success: function(msg) {
				var select = $('div#select_company');
				e.removeClass('loading');
				select.html(msg);
				select.dialog({
					resizable: true,
					height: 500,
					width: 500,
					modal: true,
					buttons: {
						'Sélectionner': function(){
							var checked = $('input:radio:checked');
							if (checked.length <= 0) {
								$('input[name=studentactivity-company]').val('');
								$('input[name=studentactivity-companyId]').val('');
							} else {
								$('input[name=studentactivity-company]').val($('span#company-'+checked.first().val()).html());
								$('input[name=studentactivity-companyId]').val(checked.first().val());
							}
							$(this).dialog('close');
						},
						'Annuler': function(){
							$(this).dialog('close');
						}
					}
				});
			}
		});
	});
	
	// Case: submitting a new (or edited) studentactivity
	$('a#form-studentactivity').click(function(){
		
		// Getting inputs
		var operation = $('input[name=studentactivity-operation]'),
			id = $('input[name=studentactivity-studentactivityId]'),
			contact = $('input[name=studentactivity-contactId]'),
			company = $('input[name=studentactivity-companyId]'),
			studentactivity = $('select[name=studentactivity-role]'),
			from = $('input[name=studentactivity-from]'),
			to = $('input[name=studentactivity-to]'),
			content = $('textarea[name=studentactivity-content]'),
			remuntype = $('select[name=studentactivity-remuntype]'),
			remunamount = $('input[name=studentactivity-remunamount]'),
			remunid = $('input[name=studentactivity-remunerationId]'),
			allFields = $([]).add(contact).add(company).add(from).add(to).add(remunamount);
		
		// Checking rights
		if (check_credentials('gotstudentactivity_'+operation.val()) != 'yes'){
			alert('Vous n\'avez pas le droit d\'effectuer cette opération !');
			return false;
		}
		
		// Checking form validity
		allFields.removeClass('ui-state-error');
		var valid = true;
		valid = valid && check_length(contact, 'Contact', 1, 10);
		valid = valid && check_length(company, 'Société', 1, 10);
		valid = valid && check_length(from, 'De', 1, 10);
		valid = valid && check_length(to, 'À', 1, 10);
		valid = valid && check_regexp(from, rexp_date, 'Format de date : jj/mm/aaaa');
		valid = valid && check_regexp(to, rexp_date, 'Format de date : jj/mm/aaaa');
		valid = valid && check_dates(from, to, 'Le champ \'De\' doit être antérieur au champ \'À\'.');
		if (valid && (remunamount.val().length > 0)) {
			if (remunamount.val() < 0) {
				remunamount.addClass('ui-state-error');
				valid = false;
				alert('Le champ \'Montant\' doît être supérieur à 0');
			}
		}
		
		// We have errors
		if (!valid)
			return false;

		// No error: AJAX processing
		showLoading();
		$.ajax({
			type:	'POST',
			url:	'process/gotstudentactivity_'+operation.val()+'.php',
			data:	'id='+id.val()+'&contact='+contact.val()+'&company='+company.val()+'&studentactivity='+studentactivity.val()+'&from='+from.val()+'&content='+content.val()+'&to='+to.val()+'&remuntype='+remuntype.val()+'&remunamount='+remunamount.val()+'&remunid='+remunid.val(),
			success: function(msg){
				addLoading(100);
				history.back();
			}
		});
		return false;
	});
});